#pragma once
#include<assert.h>
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;


namespace cyq
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;


		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const iterator begin() const
		{
			return _str;
		}

		const iterator end() const
		{
			return _str + _size;
		}

		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
			, _str(new char[_capacity + 1])
		{
			memcpy(_str, str, sizeof(char) * (_size + 1));
		}


		//可以拷贝构造到'\0'
		string(const string& str)
		{
			_str = new char[str._capacity + 1];
			memcpy(_str, str._str, sizeof(char) * (str._size + 1));
			_size = str._size;
			_capacity = str._capacity;
		}

		//交换要改变 必须传引用
		void swap(string& str)
		{
			std::swap(_str, str._str);
			std::swap(_size, str._size);
			std::swap(_capacity, str._capacity);
		}

		//不给初始化列表调用默认拷贝构造时内置类型可能会被初始化成随机值（调用析构函数会出问题)
		//不可以拷贝构造到'\0'
		/*string(const string& str)
			:_str(nullptr)
			,_size(0)
			,_capacity(0)
		{
			string temp(str._str);
			swap(temp);
		}*/

		~string()
		{
			if (_str)
			{
				delete[] _str;
				_str = nullptr;
				_size = _capacity = 0;
			}

		}

		//赋值运算符重载 深拷贝
		/*string& operator=(const string& str)
		{
			if (*this != str)
			{
				char* temp = new char[str._capacity + 1];
				memcpy(temp, str._str, str._size);
				delete[] _str;
				_str = temp;
				_size = str._size;
				_capacity = str._capacity;
			}

			return *this;
		}*/



		//*this的原空间会因为交换被temp出函数作用域后带走销毁
		/*string& operator=(const string& str)
		{
			if (*this != str)
			{
				string temp(str);
				swap(temp);

			}
			return *this;
		}*/

		string& operator=(string temp)
		{
			swap(temp);
			return *this;
		}


		char* c_str() const
		{
			return _str;
		}

		size_t size() const
		{
			return _size;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size && pos >= 0);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size && pos >= 0);
			return _str[pos];
		}

		//修改容量
		void reserve(int n)
		{
			if (n > _capacity)
			{
				char* temp = new char[n + 1];  //给'\0'留空间
				memcpy(temp, _str, sizeof(char) * (_size + 1));
				delete[] _str;
				_str = temp;
				_capacity = n;
			}
		}

		//在扩容时先不需要考虑容量+1('\0')  因为除了push_back和append需要扩容 也有一些功能的实现需要扩容 在reserve修改容量时会考虑到'\0'
		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				//2倍扩容
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}
			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				//至少扩容到_size+len
				reserve(_size + len);
			}
			memcpy(_str + _size, str, sizeof(char) * (len + 1));
			_size += len;	
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		string& operator+=(const char* ch)
		{
			append(ch);
			return *this;
		}

		void insert(size_t pos, size_t n, char ch)
		{
			assert(pos <= _size);

			if (_size + n > _capacity)
			{
				//至少扩容到_size+n
				reserve(_size + n);
			}

			int end = _size;
			//注意整型提升
			while (end >= pos&&end!=npos)
			{
				_str[end + n] = _str[end];
				end--;
			}

			for (size_t i = 0; i < n; i++)
			{
				_str[pos + i] = ch;
			}
			_size += n;
			
		}

		void insert(size_t pos, const char* str)
		{
			assert(pos <= _size);

			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				//至少扩容到_size+len
				reserve(_size + len);
			}

			int end = _size;
			//注意整型提升
			while (end >= pos && end != npos)
			{
				_str[end + len] = _str[end];
				end--;
			}
			/*for (size_t i = 0; i < len; i++)
			{
				_str[pos + i] = str[i];
			}
			_size += len;*/
			memcpy(_str + pos, str, sizeof(char) * len);
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos <= _size);

			if (len == npos || len + pos >= _size)
			{
				_str[pos] = '\0';
				_size = pos;
				_str[_size] = '\0';
			}
			else
			{
				int end = pos + len;
				while (end <= _size)
				{
					_str[pos++] = _str[end++];
				}
				_size -= len;
			}
		}

		size_t find(char ch, size_t pos = 0)
		{
			assert(pos < _size);

			for (size_t i = 0; i < _size;i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}
				else
					return npos;
			}
		}

		size_t find(const char* str, size_t pos = 0)
		{
			assert(pos < _size);

			const char* ptr = strstr(_str + pos, str);
			if (ptr)
			{
				return ptr - _str;
			}
			else
			{
				return npos;
			}
		}

		//提取子串
		string substr(size_t pos, size_t len = npos)
		{
			assert(pos > _size);

			//求子串的长度
			size_t n = len;
			if (len == npos || pos + len > _size)
			{
				n = _size-pos;
			}
			string temp;
			temp.reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				temp += _str[i+pos];
			}

			return temp;
		}

		void resize(size_t n, char ch = '\0')
		{
			if (n < _size)
			{
				_size = n;
				_str[_size] = '\0';
			}
			else
			{
				reserve(n);
				for (size_t i = _size; i < n; i++)
				{
					_str[i] = ch;
				}
				_size = n;
				_str[_size] = '\0';
			}
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		bool operator<(const string& str) const
		{
			//等于返回0 大于返回大于0 小于返回小于0
			int ret = memcmp(_str, str._str, _size < str._size ? _capacity : str._size);

			//"hello" "hello"
			//"helloxx" "hello"
			//"hello" "helloxx"

			//ret==0说明最小长度字符串跟另一个字符串前面的部分是相等的
			return ret == 0 ? _size < str._size : ret<0;
		}

		//bool operator<(const string& str) const
		//{
		//	int s1 = 0, s2 = 0;
		//	while (s1 < _size && s2 < str._size)
		//	{
		//		if (_str[s1] < str._str[s2])
		//		{
		//			return true;
		//		}
		//		else if (_str[s1] > str._str[s2])
		//		{
		//			return false;
		//		}
		//		else
		//		{
		//			s1++;
		//			s2++;
		//		}
		//	}

		//	//"hello" "hello"
		//	//"helloxx" "hello"
		//	//"hello" "helloxx"
		//	return _size < str._size;
		//	
		//}

		bool operator==(const string& str) const
		{
			return _size == str._size && (memcmp(_str, str._str, _size) == 0);
		}

		bool operator<=(const string& str) const
		{
			return (*this < str) || (*this == str);
		}

		bool operator>(const string& str) const
		{
			return !(*this<=str);

		}
	
		bool operator>=(const string& str) const
		{
			return (*this > str) || (*this == str);
		}

		bool operator!=(const string& str) const
		{
			return !(*this == str);
		}


	private:
		size_t _size;
		size_t _capacity;
		char* _str;
		static size_t npos;
	};

	size_t string::npos = -1;
}

ostream& operator<<(ostream& out, const cyq::string& str)
{
	for (auto e : str)
	{
		out << e;
	}
	return out;
}

istream& operator>>(istream& in, cyq::string& str)
{
	//每次再输入时要清空内容
	str.clear();
	//确保可以读到空格和换行符
	char ch = in.get();
	//清除字符串前面的换行和空格符
	while (ch == ' ' || ch == '\n')
	{
		ch=in.get();
	}
	char temp[128];
	int i = 0;
	while (ch != ' ' && ch != '\n')
	{
		temp[i++]=ch;
		if (i == 127)
		{
			temp[i] = '\0';
			str += temp;
			i = 0;
		}
		ch = in.get();

	}
	if (i != 0)
	{
		temp[i] = '\0';
		str += temp;
	}
	/*while (ch !='\n'&& ch != ' ')
	{
		str += ch;
		ch = in.get();
	}*/
	return in;
}
